//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {

    // 产品列表
    items: [],

    // 数据加载
    loading: true,

    // 加载提示
    loadTips: '',

    // 星星数
    // star: [0, 1, 2, 3, 4],

    // 星星评分
    // starNum: [0, 0, 0, 0],

    // 选择的图片
    selectImg: []

  },

  // 页面加载
  onLoad: function (options) {
    this.setData({
      options: options
    });
    this.getData();
  },

  // 用户输入
  getTextarea: function (event) {
    console.log(event);
    var txt = event.detail.value
    console.log(txt);
    this.setData({
      textarea: txt
    })
  },

  // 验证
  verify: function (fn) {
    var that = this;
    if (!that.data.textarea) {
      that.tips('请输入评价内容');
      return false;
    } 
    return true;
  },

  // 最终提交（确定发表）
  submit: function () {
    var that = this;
    if (that.verify()) {
      that.submitData();
    }
  },

  // 提交评论信息
  submitData: function () {
    var that = this;
    app.getLoginSession(function (session) {
      console.log(that.data.options);
      wx.request({
        url: app.api,
        data: {
          app: 'evaluation_buyer',
          act: 'evaluate',
          order_id: that.data.options.order_id,
          description_score: 1,
          performance_cost: 1,
          service_attitude:1,
          delivery_speed: 1,
          buyer_comment: that.data.textarea,
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data;
          console.log(data);

          if (data.done) {
            wx.showToast({
              title: data.retval.msg
            });
            wx.navigateTo({
              url: '/pages/evalSuccess/evalSuccess',
            })
          } else {
            wx.showToast({
              title: '提交失败'
            })
          }
        }
      })
    })
  },

  // 上传图片
  uploadImg: function (img, fn) {
    var that = this;
    app.getLoginSession(function (session) {
      console.log(img[0]);
      wx.uploadFile({
        url: app.api,
        filePath: img[0],
        file: 'file',
        formData: {
          app: 'swfupload_fe',
          act: 'index',
          comment_img: img[0],
          t_session: session
        },
        method: 'POST',
        header: {
          'Content-Type': 'multipart/form-data'
        },
        success: function (res) {
          var data = res.data;
          console.log(data);
          if (data.done) {
            //typeof fn == 'function' && fn(data);
          } else {
            that.tips('图片上传失败')
          }
        }
      })
    })
  },

  // 提示框
  tips: function (txt) {
    wx.showModal({
      title: '提示',
      content: txt,
      showCancel: false
    })
  },

  // 选择图片
  chooseImg: function () {
    var that = this;
    var selectImg = that.data.selectImg;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {

        var tempFilePaths = res.tempFilePaths

        if (tempFilePaths.length + selectImg.length > 5) {
          that.tips('最多只能上传5张')
        } else {
          for (var i = 0; i < tempFilePaths.length; i++) {
            selectImg.push(tempFilePaths[i])
          }
          that.setData({
            selectImg: selectImg
          })
          console.log(selectImg);
        }

      },
      fail: function () {
        that.tips('选取图片失败')
      }
    })
  },

  // 删除图片
  deleteImg: function (event) {
    var that = this;
    var selectImg = that.data.selectImg;
    var url = event.currentTarget.dataset.url;
    for (var i in selectImg) {
      if (selectImg[i] == url) {
        selectImg.splice(i, 1);
        break
      }
    }
    that.setData({
      selectImg: selectImg
    });
    console.log(that.data.selectImg);
  },

  // 获取数据
  getData: function () {

    var that = this;

    app.getLoginSession(function (session) {
      wx.request({

        // 请求地址
        url: app.api,

        // 请求参数
        data: {
          app: 'evaluation_buyer',
          act: 'evaluate',
          order_id: that.data.options.order_id,
          t_session: session
        },

        // 请求成功
        success: function (res) {
          var data = res.data;
          console.log(data);
          if (data.done) {
            if (data.retval && data.retval.goods && data.retval.goods.length) {
              that.setData({
                loading: false,
                data: data.retval.goods
              })
            } else {
              that.setData({
                errorTips: '无订单数据'
              })
            }
          } else {
            that.setData({
              errorTips: data.msg || '请求错误，act：index'
            })
          }
        },
        fail: function () {
          that.setData({
            errorTips: '请求错误'
          })
        }
      })
    })
  }

};

// 注册页面
Page(init);
